<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ib_insync.ib &mdash; ib_insync 0.9.70 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.io_modules/ib_insync/ib.html"/>
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>ib_insync.ib</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ib_insync.ib</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;High-level interface to Interactive Brokers.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Awaitable</span><span class="p">,</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Iterator</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Union</span>

<span class="kn">from</span> <span class="nn">eventkit</span> <span class="kn">import</span> <span class="n">Event</span>

<span class="kn">import</span> <span class="nn">ib_insync.util</span> <span class="k">as</span> <span class="nn">util</span>
<span class="kn">from</span> <span class="nn">ib_insync.client</span> <span class="kn">import</span> <span class="n">Client</span>
<span class="kn">from</span> <span class="nn">ib_insync.contract</span> <span class="kn">import</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">ContractDescription</span><span class="p">,</span> <span class="n">ContractDetails</span>
<span class="kn">from</span> <span class="nn">ib_insync.objects</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">AccountValue</span><span class="p">,</span> <span class="n">BarDataList</span><span class="p">,</span> <span class="n">DepthMktDataDescription</span><span class="p">,</span> <span class="n">Execution</span><span class="p">,</span>
    <span class="n">ExecutionFilter</span><span class="p">,</span> <span class="n">Fill</span><span class="p">,</span> <span class="n">HistogramData</span><span class="p">,</span> <span class="n">HistoricalNews</span><span class="p">,</span> <span class="n">NewsArticle</span><span class="p">,</span>
    <span class="n">NewsBulletin</span><span class="p">,</span> <span class="n">NewsProvider</span><span class="p">,</span> <span class="n">NewsTick</span><span class="p">,</span> <span class="n">OptionChain</span><span class="p">,</span> <span class="n">OptionComputation</span><span class="p">,</span>
    <span class="n">PnL</span><span class="p">,</span> <span class="n">PnLSingle</span><span class="p">,</span> <span class="n">PortfolioItem</span><span class="p">,</span> <span class="n">Position</span><span class="p">,</span> <span class="n">PriceIncrement</span><span class="p">,</span>
    <span class="n">RealTimeBarList</span><span class="p">,</span> <span class="n">ScanDataList</span><span class="p">,</span> <span class="n">ScannerSubscription</span><span class="p">,</span> <span class="n">TagValue</span><span class="p">,</span>
    <span class="n">TradeLogEntry</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">ib_insync.order</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">BracketOrder</span><span class="p">,</span> <span class="n">LimitOrder</span><span class="p">,</span> <span class="n">Order</span><span class="p">,</span> <span class="n">OrderState</span><span class="p">,</span> <span class="n">OrderStatus</span><span class="p">,</span> <span class="n">StopOrder</span><span class="p">,</span> <span class="n">Trade</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">ib_insync.ticker</span> <span class="kn">import</span> <span class="n">Ticker</span>
<span class="kn">from</span> <span class="nn">ib_insync.wrapper</span> <span class="kn">import</span> <span class="n">Wrapper</span>


<div class="viewcode-block" id="IB"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB">[docs]</a><span class="k">class</span> <span class="nc">IB</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provides both a blocking and an asynchronous interface</span>
<span class="sd">    to the IB API, using asyncio networking and event loop.</span>

<span class="sd">    The IB class offers direct access to the current state, such as</span>
<span class="sd">    orders, executions, positions, tickers etc. This state is</span>
<span class="sd">    automatically kept in sync with the TWS/IBG application.</span>

<span class="sd">    This class has most request methods of EClient, with the</span>
<span class="sd">    same names and parameters (except for the reqId parameter</span>
<span class="sd">    which is not needed anymore).</span>
<span class="sd">    Request methods that return a result come in two versions:</span>

<span class="sd">    * Blocking: Will block until complete and return the result.</span>
<span class="sd">      The current state will be kept updated while the request is ongoing;</span>

<span class="sd">    * Asynchronous: All methods that have the &quot;Async&quot; postfix.</span>
<span class="sd">      Implemented as coroutines or methods that return a Future and</span>
<span class="sd">      intended for advanced users.</span>

<span class="sd">    **The One Rule:**</span>

<span class="sd">    While some of the request methods are blocking from the perspective</span>
<span class="sd">    of the user, the framework will still keep spinning in the background</span>
<span class="sd">    and handle all messages received from TWS/IBG. It is important to</span>
<span class="sd">    not block the framework from doing its work. If, for example,</span>
<span class="sd">    the user code spends much time in a calculation, or uses time.sleep()</span>
<span class="sd">    with a long delay, the framework will stop spinning, messages</span>
<span class="sd">    accumulate and things may go awry.</span>

<span class="sd">    The one rule when working with the IB class is therefore that</span>

<span class="sd">    **user code may not block for too long**.</span>

<span class="sd">    To be clear, the IB request methods are okay to use and do not</span>
<span class="sd">    count towards the user operation time, no matter how long the</span>
<span class="sd">    request takes to finish.</span>

<span class="sd">    So what is &quot;too long&quot;? That depends on the situation. If, for example,</span>
<span class="sd">    the timestamp of tick data is to remain accurate within a millisecond,</span>
<span class="sd">    then the user code must not spend longer than a millisecond. If, on</span>
<span class="sd">    the other extreme, there is very little incoming data and there</span>
<span class="sd">    is no desire for accurate timestamps, then the user code can block</span>
<span class="sd">    for hours.</span>

<span class="sd">    If a user operation takes a long time then it can be farmed out</span>
<span class="sd">    to a different process.</span>
<span class="sd">    Alternatively the operation can be made such that it periodically</span>
<span class="sd">    calls IB.sleep(0); This will let the framework handle any pending</span>
<span class="sd">    work and return when finished. The operation should be aware</span>
<span class="sd">    that the current state may have been updated during the sleep(0) call.</span>

<span class="sd">    For introducing a delay, never use time.sleep() but use</span>
<span class="sd">    :meth:`.sleep` instead.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        RequestTimeout (float): Timeout (in seconds) to wait for a</span>
<span class="sd">          blocking request to finish before raising ``asyncio.TimeoutError``.</span>
<span class="sd">          The default value of 0 will wait indefinitely.</span>
<span class="sd">          Note: This timeout is not used for the ``*Async`` methods.</span>
<span class="sd">        RaiseRequestErrors (bool):</span>
<span class="sd">          Specifies the behaviour when certain API requests fail:</span>

<span class="sd">          * :data:`False`: Silently return an empty result;</span>
<span class="sd">          * :data:`True`: Raise a :class:`.RequestError`.</span>
<span class="sd">        MaxSyncedSubAccounts (int): Do not use sub-account updates</span>
<span class="sd">          if the number of sub-accounts exceeds this number (50 by default).</span>
<span class="sd">        TimezoneTWS (pytz.timezone): Specifies what timezone TWS (or gateway)</span>
<span class="sd">          is using. The default is to assume local system timezone.</span>

<span class="sd">    Events:</span>
<span class="sd">        * ``connectedEvent`` ():</span>
<span class="sd">          Is emitted after connecting and synchronzing with TWS/gateway.</span>

<span class="sd">        * ``disconnectedEvent`` ():</span>
<span class="sd">          Is emitted after disconnecting from TWS/gateway.</span>

<span class="sd">        * ``updateEvent`` ():</span>
<span class="sd">          Is emitted after a network packet has been handeled.</span>

<span class="sd">        * ``pendingTickersEvent`` (tickers: Set[:class:`.Ticker`]):</span>
<span class="sd">          Emits the set of tickers that have been updated during the last</span>
<span class="sd">          update and for which there are new ticks, tickByTicks or domTicks.</span>

<span class="sd">        * ``barUpdateEvent`` (bars: :class:`.BarDataList`,</span>
<span class="sd">          hasNewBar: bool): Emits the bar list that has been updated in</span>
<span class="sd">          real time. If a new bar has been added then hasNewBar is True,</span>
<span class="sd">          when the last bar has changed it is False.</span>

<span class="sd">        * ``newOrderEvent`` (trade: :class:`.Trade`):</span>
<span class="sd">          Emits a newly placed trade.</span>

<span class="sd">        * ``orderModifyEvent`` (trade: :class:`.Trade`):</span>
<span class="sd">          Emits when order is modified.</span>

<span class="sd">        * ``cancelOrderEvent`` (trade: :class:`.Trade`):</span>
<span class="sd">          Emits a trade directly after requesting for it to be cancelled.</span>

<span class="sd">        * ``openOrderEvent`` (trade: :class:`.Trade`):</span>
<span class="sd">          Emits the trade with open order.</span>

<span class="sd">        * ``orderStatusEvent`` (trade: :class:`.Trade`):</span>
<span class="sd">          Emits the changed order status of the ongoing trade.</span>

<span class="sd">        * ``execDetailsEvent`` (trade: :class:`.Trade`, fill: :class:`.Fill`):</span>
<span class="sd">          Emits the fill together with the ongoing trade it belongs to.</span>

<span class="sd">        * ``commissionReportEvent`` (trade: :class:`.Trade`,</span>
<span class="sd">          fill: :class:`.Fill`, report: :class:`.CommissionReport`):</span>
<span class="sd">          The commission report is emitted after the fill that it belongs to.</span>

<span class="sd">        * ``updatePortfolioEvent`` (item: :class:`.PortfolioItem`):</span>
<span class="sd">          A portfolio item has changed.</span>

<span class="sd">        * ``positionEvent`` (position: :class:`.Position`):</span>
<span class="sd">          A position has changed.</span>

<span class="sd">        * ``accountValueEvent`` (value: :class:`.AccountValue`):</span>
<span class="sd">          An account value has changed.</span>

<span class="sd">        * ``accountSummaryEvent`` (value: :class:`.AccountValue`):</span>
<span class="sd">          An account value has changed.</span>

<span class="sd">        * ``pnlEvent`` (entry: :class:`.PnL`):</span>
<span class="sd">          A profit- and loss entry is updated.</span>

<span class="sd">        * ``pnlSingleEvent`` (entry: :class:`.PnLSingle`):</span>
<span class="sd">          A profit- and loss entry for a single position is updated.</span>

<span class="sd">        * ``tickNewsEvent`` (news: :class:`.NewsTick`):</span>
<span class="sd">          Emit a new news headline.</span>

<span class="sd">        * ``newsBulletinEvent`` (bulletin: :class:`.NewsBulletin`):</span>
<span class="sd">          Emit a new news bulletin.</span>

<span class="sd">        * ``scannerDataEvent`` (data: :class:`.ScanDataList`):</span>
<span class="sd">          Emit data from a scanner subscription.</span>

<span class="sd">        * ``errorEvent`` (reqId: int, errorCode: int, errorString: str,</span>
<span class="sd">          contract: :class:`.Contract`):</span>
<span class="sd">          Emits the reqId/orderId and TWS error code and string (see</span>
<span class="sd">          https://interactivebrokers.github.io/tws-api/message_codes.html)</span>
<span class="sd">          together with the contract the error applies to (or None if no</span>
<span class="sd">          contract applies).</span>

<span class="sd">        * ``timeoutEvent`` (idlePeriod: float):</span>
<span class="sd">          Is emitted if no data is received for longer than the timeout period</span>
<span class="sd">          specified with :meth:`.setTimeout`. The value emitted is the period</span>
<span class="sd">          in seconds since the last update.</span>

<span class="sd">        Note that it is not advisable to place new requests inside an event</span>
<span class="sd">        handler as it may lead to too much recursion.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">events</span> <span class="o">=</span> <span class="p">(</span>
        <span class="s1">&#39;connectedEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;disconnectedEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;updateEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;pendingTickersEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;barUpdateEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;newOrderEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;orderModifyEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;cancelOrderEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;openOrderEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;orderStatusEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;execDetailsEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;commissionReportEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;updatePortfolioEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;positionEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;accountValueEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;accountSummaryEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;pnlEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;pnlSingleEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;scannerDataEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;tickNewsEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;newsBulletinEvent&#39;</span><span class="p">,</span>
        <span class="s1">&#39;errorEvent&#39;</span><span class="p">,</span> <span class="s1">&#39;timeoutEvent&#39;</span><span class="p">)</span>

    <span class="n">RequestTimeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">RaiseRequestErrors</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">MaxSyncedSubAccounts</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">50</span>
    <span class="n">TimezoneTWS</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_createEvents</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span> <span class="o">=</span> <span class="n">Wrapper</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span> <span class="o">=</span> <span class="n">Client</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">errorEvent</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_onError</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">apiEnd</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">disconnectedEvent</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;ib_insync.ib&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_createEvents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connectedEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;connectedEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disconnectedEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;disconnectedEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updateEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;updateEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickersEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;pendingTickersEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">barUpdateEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;barUpdateEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">newOrderEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;newOrderEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">orderModifyEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;orderModifyEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cancelOrderEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;cancelOrderEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">openOrderEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;openOrderEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">orderStatusEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;orderStatusEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">execDetailsEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;execDetailsEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">commissionReportEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;commissionReportEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updatePortfolioEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;updatePortfolioEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">positionEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;positionEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accountValueEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;accountValueEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accountSummaryEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;accountSummaryEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pnlEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;pnlEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pnlSingleEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;pnlSingleEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scannerDataEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;scannerDataEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tickNewsEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;tickNewsEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">newsBulletinEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;newsBulletinEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">errorEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;errorEvent&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeoutEvent</span> <span class="o">=</span> <span class="n">Event</span><span class="p">(</span><span class="s1">&#39;timeoutEvent&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="fm">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">_exc</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">conn</span> <span class="o">=</span> <span class="p">(</span><span class="sa">f</span><span class="s1">&#39;connected to </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">host</span><span class="si">}</span><span class="s1">:&#39;</span>
                <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">port</span><span class="si">}</span><span class="s1"> clientId=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">clientId</span><span class="si">}</span><span class="s1">&#39;</span> <span class="k">if</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">isConnected</span><span class="p">()</span> <span class="k">else</span> <span class="s1">&#39;not connected&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;&lt;</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__qualname__</span><span class="si">}</span><span class="s1"> </span><span class="si">{</span><span class="n">conn</span><span class="si">}</span><span class="s1">&gt;&#39;</span>

<div class="viewcode-block" id="IB.connect"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.connect">[docs]</a>    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
            <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span> <span class="n">readonly</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Connect to a running TWS or IB gateway application.</span>
<span class="sd">        After the connection is made the client is fully synchronized</span>
<span class="sd">        and ready to serve requests.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            host: Host name or IP address.</span>
<span class="sd">            port: Port number.</span>
<span class="sd">            clientId: ID number to use for this client; must be unique per</span>
<span class="sd">              connection. Setting clientId=0 will automatically merge manual</span>
<span class="sd">              TWS trading with this client.</span>
<span class="sd">            timeout: If establishing the connection takes longer than</span>
<span class="sd">              ``timeout`` seconds then the ``asyncio.TimeoutError`` exception</span>
<span class="sd">              is raised. Set to 0 to disable timeout.</span>
<span class="sd">            readonly: Set to ``True`` when API is in read-only mode.</span>
<span class="sd">            account: Main account to receive updates for.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">connectAsync</span><span class="p">(</span>
            <span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">clientId</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">readonly</span><span class="p">,</span> <span class="n">account</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.disconnect"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.disconnect">[docs]</a>    <span class="k">def</span> <span class="nf">disconnect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Disconnect from a TWS or IB gateway application.</span>
<span class="sd">        This will clear all session state.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">isConnected</span><span class="p">():</span>
            <span class="k">return</span>
        <span class="n">stats</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">connectionStats</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
            <span class="sa">f</span><span class="s1">&#39;Disconnecting from </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">host</span><span class="si">}</span><span class="s1">:</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">port</span><span class="si">}</span><span class="s1">, &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">util</span><span class="o">.</span><span class="n">formatSI</span><span class="p">(</span><span class="n">stats</span><span class="o">.</span><span class="n">numBytesSent</span><span class="p">)</span><span class="si">}</span><span class="s1">B sent &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;in </span><span class="si">{</span><span class="n">stats</span><span class="o">.</span><span class="n">numMsgSent</span><span class="si">}</span><span class="s1"> messages, &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">util</span><span class="o">.</span><span class="n">formatSI</span><span class="p">(</span><span class="n">stats</span><span class="o">.</span><span class="n">numBytesRecv</span><span class="p">)</span><span class="si">}</span><span class="s1">B received &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;in </span><span class="si">{</span><span class="n">stats</span><span class="o">.</span><span class="n">numMsgRecv</span><span class="si">}</span><span class="s1"> messages, &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;session time </span><span class="si">{</span><span class="n">util</span><span class="o">.</span><span class="n">formatSI</span><span class="p">(</span><span class="n">stats</span><span class="o">.</span><span class="n">duration</span><span class="p">)</span><span class="si">}</span><span class="s1">s.&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">disconnectedEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span></div>

<div class="viewcode-block" id="IB.isConnected"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.isConnected">[docs]</a>    <span class="k">def</span> <span class="nf">isConnected</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Is there an API connection to TWS or IB gateway?&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">isReady</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_onError</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">,</span> <span class="n">errorString</span><span class="p">,</span> <span class="n">contract</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">errorCode</span> <span class="o">==</span> <span class="mi">1102</span><span class="p">:</span>
            <span class="c1"># &quot;Connectivity between IB and Trader Workstation has been</span>
            <span class="c1"># restored&quot;: Resubscribe to account summary.</span>
            <span class="n">asyncio</span><span class="o">.</span><span class="n">ensure_future</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqAccountSummaryAsync</span><span class="p">())</span>

    <span class="n">run</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">run</span><span class="p">)</span>
    <span class="n">schedule</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">schedule</span><span class="p">)</span>
    <span class="n">sleep</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">sleep</span><span class="p">)</span>
    <span class="n">timeRange</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">timeRange</span><span class="p">)</span>
    <span class="n">timeRangeAsync</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">timeRangeAsync</span><span class="p">)</span>
    <span class="n">waitUntil</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">util</span><span class="o">.</span><span class="n">waitUntil</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_run</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">awaitables</span><span class="p">:</span> <span class="n">Awaitable</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">util</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="o">*</span><span class="n">awaitables</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">RequestTimeout</span><span class="p">)</span>

<div class="viewcode-block" id="IB.waitOnUpdate"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.waitOnUpdate">[docs]</a>    <span class="k">def</span> <span class="nf">waitOnUpdate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Wait on any new update to arrive from the network.</span>

<span class="sd">        Args:</span>
<span class="sd">            timeout: Maximum time in seconds to wait.</span>
<span class="sd">                If 0 then no timeout is used.</span>

<span class="sd">        .. note::</span>
<span class="sd">            A loop with ``waitOnUpdate`` should not be used to harvest</span>
<span class="sd">            tick data from tickers, since some ticks can go missing.</span>
<span class="sd">            This happens when multiple updates occur almost simultaneously;</span>
<span class="sd">            The ticks from the first update are then cleared.</span>
<span class="sd">            Use events instead to prevent this.</span>

<span class="sd">        Returns:</span>
<span class="sd">            ``True`` if not timed-out, ``False`` otherwise.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">timeout</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">util</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">updateEvent</span><span class="p">,</span> <span class="n">timeout</span><span class="p">))</span>
            <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">util</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">updateEvent</span><span class="p">)</span>
        <span class="k">return</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="IB.loopUntil"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.loopUntil">[docs]</a>    <span class="k">def</span> <span class="nf">loopUntil</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">condition</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Iterator</span><span class="p">[</span><span class="nb">object</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Iterate until condition is met, with optional timeout in seconds.</span>
<span class="sd">        The yielded value is that of the condition or False when timed out.</span>

<span class="sd">        Args:</span>
<span class="sd">            condition: Predicate function that is tested after every network</span>
<span class="sd">            update.</span>
<span class="sd">            timeout: Maximum time in seconds to wait.</span>
<span class="sd">                If 0 then no timeout is used.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">endTime</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">+</span> <span class="n">timeout</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">test</span> <span class="o">=</span> <span class="n">condition</span> <span class="ow">and</span> <span class="n">condition</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">test</span><span class="p">:</span>
                <span class="k">yield</span> <span class="n">test</span>
                <span class="k">return</span>
            <span class="k">elif</span> <span class="n">timeout</span> <span class="ow">and</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">endTime</span><span class="p">:</span>
                <span class="k">yield</span> <span class="kc">False</span>
                <span class="k">return</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">yield</span> <span class="n">test</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">waitOnUpdate</span><span class="p">(</span><span class="n">endTime</span> <span class="o">-</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="k">if</span> <span class="n">timeout</span> <span class="k">else</span> <span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.setTimeout"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.setTimeout">[docs]</a>    <span class="k">def</span> <span class="nf">setTimeout</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">60</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set a timeout for receiving messages from TWS/IBG, emitting</span>
<span class="sd">        ``timeoutEvent`` if there is no incoming data for too long.</span>

<span class="sd">        The timeout fires once per connected session but can be set again</span>
<span class="sd">        after firing or after a reconnect.</span>

<span class="sd">        Args:</span>
<span class="sd">            timeout: Timeout in seconds.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">setTimeout</span><span class="p">(</span><span class="n">timeout</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.managedAccounts"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.managedAccounts">[docs]</a>    <span class="k">def</span> <span class="nf">managedAccounts</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of account names.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">accounts</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.accountValues"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.accountValues">[docs]</a>    <span class="k">def</span> <span class="nf">accountValues</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountValue</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of account values for the given account,</span>
<span class="sd">        or of all accounts if account is left blank.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">account</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">accountValues</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">v</span><span class="o">.</span><span class="n">account</span> <span class="o">==</span> <span class="n">account</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">accountValues</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.accountSummary"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.accountSummary">[docs]</a>    <span class="k">def</span> <span class="nf">accountSummary</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">AccountValue</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of account values for the given account,</span>
<span class="sd">        or of all accounts if account is left blank.</span>

<span class="sd">        This method is blocking on first run, non-blocking after that.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">accountSummaryAsync</span><span class="p">(</span><span class="n">account</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.portfolio"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.portfolio">[docs]</a>    <span class="k">def</span> <span class="nf">portfolio</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">PortfolioItem</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of portfolio items of the default account.&quot;&quot;&quot;</span>
        <span class="n">account</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">accounts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">portfolio</span><span class="p">[</span><span class="n">account</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span></div>

<div class="viewcode-block" id="IB.positions"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.positions">[docs]</a>    <span class="k">def</span> <span class="nf">positions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Position</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of positions for the given account,</span>
<span class="sd">        or of all accounts if account is left blank.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">account</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">positions</span><span class="p">[</span><span class="n">account</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">positions</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                    <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span></div>

<div class="viewcode-block" id="IB.pnl"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.pnl">[docs]</a>    <span class="k">def</span> <span class="nf">pnl</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">modelCode</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">PnL</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of subscribed :class:`.PnL` objects (profit and loss),</span>
<span class="sd">        optionally filtered by account and/or modelCode.</span>

<span class="sd">        The :class:`.PnL` objects are kept live updated.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">            modelCode: If specified, filter for this account model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnL</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> <span class="k">if</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">account</span> <span class="ow">or</span> <span class="n">v</span><span class="o">.</span><span class="n">account</span> <span class="o">==</span> <span class="n">account</span><span class="p">)</span>
                <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">modelCode</span> <span class="ow">or</span> <span class="n">v</span><span class="o">.</span><span class="n">modelCode</span> <span class="o">==</span> <span class="n">modelCode</span><span class="p">)]</span></div>

<div class="viewcode-block" id="IB.pnlSingle"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.pnlSingle">[docs]</a>    <span class="k">def</span> <span class="nf">pnlSingle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
            <span class="n">conId</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">PnLSingle</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of subscribed :class:`.PnLSingle` objects (profit and loss for</span>
<span class="sd">        single positions).</span>

<span class="sd">        The :class:`.PnLSingle` objects are kept live updated.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">            modelCode: If specified, filter for this account model.</span>
<span class="sd">            conId: If specified, filter for this contract ID.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnlSingle</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> <span class="k">if</span>
                <span class="p">(</span><span class="ow">not</span> <span class="n">account</span> <span class="ow">or</span> <span class="n">v</span><span class="o">.</span><span class="n">account</span> <span class="o">==</span> <span class="n">account</span><span class="p">)</span>
                <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">modelCode</span> <span class="ow">or</span> <span class="n">v</span><span class="o">.</span><span class="n">modelCode</span> <span class="o">==</span> <span class="n">modelCode</span><span class="p">)</span>
                <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="n">conId</span> <span class="ow">or</span> <span class="n">v</span><span class="o">.</span><span class="n">conId</span> <span class="o">==</span> <span class="n">conId</span><span class="p">)]</span></div>

<div class="viewcode-block" id="IB.trades"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.trades">[docs]</a>    <span class="k">def</span> <span class="nf">trades</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Trade</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all order trades from this session.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.openTrades"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.openTrades">[docs]</a>    <span class="k">def</span> <span class="nf">openTrades</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Trade</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all open order trades.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">v</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">DoneStates</span><span class="p">]</span></div>

<div class="viewcode-block" id="IB.orders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.orders">[docs]</a>    <span class="k">def</span> <span class="nf">orders</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all orders from this session.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span>
            <span class="n">trade</span><span class="o">.</span><span class="n">order</span> <span class="k">for</span> <span class="n">trade</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.openOrders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.openOrders">[docs]</a>    <span class="k">def</span> <span class="nf">openOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all open orders.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">trade</span><span class="o">.</span><span class="n">order</span> <span class="k">for</span> <span class="n">trade</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                <span class="k">if</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">DoneStates</span><span class="p">]</span></div>

<div class="viewcode-block" id="IB.fills"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.fills">[docs]</a>    <span class="k">def</span> <span class="nf">fills</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Fill</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all fills from this session.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">fills</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.executions"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.executions">[docs]</a>    <span class="k">def</span> <span class="nf">executions</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Execution</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of all executions from this session.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">fill</span><span class="o">.</span><span class="n">execution</span> <span class="k">for</span> <span class="n">fill</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">fills</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.ticker"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.ticker">[docs]</a>    <span class="k">def</span> <span class="nf">ticker</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Ticker</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get ticker of the given contract. It must have been requested before</span>
<span class="sd">        with reqMktData with the same contract object. The ticker may not be</span>
<span class="sd">        ready yet if called directly after :meth:`.reqMktData`.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to get ticker for.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">tickers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.tickers"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.tickers">[docs]</a>    <span class="k">def</span> <span class="nf">tickers</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Ticker</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Get a list of all tickers.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">tickers</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.pendingTickers"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.pendingTickers">[docs]</a>    <span class="k">def</span> <span class="nf">pendingTickers</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Ticker</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;Get a list of all tickers that have pending ticks or domTicks.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.realtimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.realtimeBars">[docs]</a>    <span class="k">def</span> <span class="nf">realtimeBars</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="n">BarDataList</span><span class="p">,</span> <span class="n">RealTimeBarList</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a list of all live updated bars. These can be 5 second realtime</span>
<span class="sd">        bars or live updated historical bars.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.newsTicks"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.newsTicks">[docs]</a>    <span class="k">def</span> <span class="nf">newsTicks</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">NewsTick</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        List of ticks with headline news.</span>
<span class="sd">        The article itself can be retrieved with :meth:`.reqNewsArticle`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">newsTicks</span></div>

<div class="viewcode-block" id="IB.newsBulletins"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.newsBulletins">[docs]</a>    <span class="k">def</span> <span class="nf">newsBulletins</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">NewsBulletin</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;List of IB news bulletins.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">msgId2NewsBulletin</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqTickers"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqTickers">[docs]</a>    <span class="k">def</span> <span class="nf">reqTickers</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">contracts</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">regulatorySnapshot</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Ticker</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request and return a list of snapshot tickers.</span>
<span class="sd">        The list is returned when all tickers are ready.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            contracts: Contracts to get tickers for.</span>
<span class="sd">            regulatorySnapshot: Request NBBO snapshots (may incur a fee).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqTickersAsync</span><span class="p">(</span>
                <span class="o">*</span><span class="n">contracts</span><span class="p">,</span> <span class="n">regulatorySnapshot</span><span class="o">=</span><span class="n">regulatorySnapshot</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.qualifyContracts"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.qualifyContracts">[docs]</a>    <span class="k">def</span> <span class="nf">qualifyContracts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">contracts</span><span class="p">:</span> <span class="n">Contract</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Contract</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Fully qualify the given contracts in-place. This will fill in</span>
<span class="sd">        the missing fields in the contract, especially the conId.</span>

<span class="sd">        Returns a list of contracts that have been successfully qualified.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            contracts: Contracts to qualify.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">qualifyContractsAsync</span><span class="p">(</span><span class="o">*</span><span class="n">contracts</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.bracketOrder"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.bracketOrder">[docs]</a>    <span class="k">def</span> <span class="nf">bracketOrder</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">quantity</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">limitPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">takeProfitPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">stopLossPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">BracketOrder</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a limit order that is bracketed by a take-profit order and</span>
<span class="sd">        a stop-loss order. Submit the bracket like:</span>

<span class="sd">        .. code-block:: python</span>

<span class="sd">            for o in bracket:</span>
<span class="sd">                ib.placeOrder(contract, o)</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/bracket_order.html</span>

<span class="sd">        Args:</span>
<span class="sd">            action: &#39;BUY&#39; or &#39;SELL&#39;.</span>
<span class="sd">            quantity: Size of order.</span>
<span class="sd">            limitPrice: Limit price of entry order.</span>
<span class="sd">            takeProfitPrice: Limit price of profit order.</span>
<span class="sd">            stopLossPrice: Stop price of loss order.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="n">action</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;BUY&#39;</span><span class="p">,</span> <span class="s1">&#39;SELL&#39;</span><span class="p">)</span>
        <span class="n">reverseAction</span> <span class="o">=</span> <span class="s1">&#39;BUY&#39;</span> <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s1">&#39;SELL&#39;</span> <span class="k">else</span> <span class="s1">&#39;SELL&#39;</span>
        <span class="n">parent</span> <span class="o">=</span> <span class="n">LimitOrder</span><span class="p">(</span>
            <span class="n">action</span><span class="p">,</span> <span class="n">quantity</span><span class="p">,</span> <span class="n">limitPrice</span><span class="p">,</span>
            <span class="n">orderId</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">(),</span>
            <span class="n">transmit</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">takeProfit</span> <span class="o">=</span> <span class="n">LimitOrder</span><span class="p">(</span>
            <span class="n">reverseAction</span><span class="p">,</span> <span class="n">quantity</span><span class="p">,</span> <span class="n">takeProfitPrice</span><span class="p">,</span>
            <span class="n">orderId</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">(),</span>
            <span class="n">transmit</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
            <span class="n">parentId</span><span class="o">=</span><span class="n">parent</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">stopLoss</span> <span class="o">=</span> <span class="n">StopOrder</span><span class="p">(</span>
            <span class="n">reverseAction</span><span class="p">,</span> <span class="n">quantity</span><span class="p">,</span> <span class="n">stopLossPrice</span><span class="p">,</span>
            <span class="n">orderId</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">(),</span>
            <span class="n">transmit</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="n">parentId</span><span class="o">=</span><span class="n">parent</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span>
            <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">BracketOrder</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">takeProfit</span><span class="p">,</span> <span class="n">stopLoss</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.oneCancelsAll"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.oneCancelsAll">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">oneCancelsAll</span><span class="p">(</span>
            <span class="n">orders</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">],</span> <span class="n">ocaGroup</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">ocaType</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Place the trades in the same One Cancels All (OCA) group.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/oca.html</span>

<span class="sd">        Args:</span>
<span class="sd">            orders: The orders that are to be placed together.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">o</span> <span class="ow">in</span> <span class="n">orders</span><span class="p">:</span>
            <span class="n">o</span><span class="o">.</span><span class="n">ocaGroup</span> <span class="o">=</span> <span class="n">ocaGroup</span>
            <span class="n">o</span><span class="o">.</span><span class="n">ocaType</span> <span class="o">=</span> <span class="n">ocaType</span>
        <span class="k">return</span> <span class="n">orders</span></div>

<div class="viewcode-block" id="IB.whatIfOrder"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.whatIfOrder">[docs]</a>    <span class="k">def</span> <span class="nf">whatIfOrder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">OrderState</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Retrieve commission and margin impact without actually</span>
<span class="sd">        placing the order. The given order will not be modified in any way.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to test.</span>
<span class="sd">            order: Order to test.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">whatIfOrderAsync</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.placeOrder"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.placeOrder">[docs]</a>    <span class="k">def</span> <span class="nf">placeOrder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Trade</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Place a new order or modify an existing order.</span>
<span class="sd">        Returns a Trade that is kept live updated with</span>
<span class="sd">        status changes, fills, etc.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to use for order.</span>
<span class="sd">            order: The order to be placed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">orderId</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">orderId</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">placeOrder</span><span class="p">(</span><span class="n">orderId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">)</span>
        <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">orderKey</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">orderId</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
        <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
            <span class="c1"># this is a modification of an existing order</span>
            <span class="k">assert</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">DoneStates</span>
            <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="s1">&#39;Modify&#39;</span><span class="p">)</span>
            <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logEntry</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;placeOrder: Modify order </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">trade</span><span class="o">.</span><span class="n">modifyEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">orderModifyEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># this is a new order</span>
            <span class="n">order</span><span class="o">.</span><span class="n">clientId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">clientId</span>
            <span class="n">order</span><span class="o">.</span><span class="n">orderId</span> <span class="o">=</span> <span class="n">orderId</span>
            <span class="n">orderStatus</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="p">(</span>
                <span class="n">orderId</span><span class="o">=</span><span class="n">orderId</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">OrderStatus</span><span class="o">.</span><span class="n">PendingSubmit</span><span class="p">)</span>
            <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>
            <span class="n">trade</span> <span class="o">=</span> <span class="n">Trade</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">orderStatus</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[</span><span class="n">logEntry</span><span class="p">])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">trade</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;placeOrder: New order </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">newOrderEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">trade</span></div>

<div class="viewcode-block" id="IB.cancelOrder"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelOrder">[docs]</a>    <span class="k">def</span> <span class="nf">cancelOrder</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Trade</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel the order and return the Trade it belongs to.</span>

<span class="sd">        Args:</span>
<span class="sd">            order: The order to be canceled.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelOrder</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="p">)</span>
        <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">orderKey</span><span class="p">(</span>
            <span class="n">order</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
        <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">trade</span><span class="o">.</span><span class="n">isDone</span><span class="p">():</span>
                <span class="n">status</span> <span class="o">=</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">status</span> <span class="o">==</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">PendingSubmit</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">order</span><span class="o">.</span><span class="n">transmit</span>
                        <span class="ow">or</span> <span class="n">status</span> <span class="o">==</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Inactive</span><span class="p">):</span>
                    <span class="n">newStatus</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Cancelled</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">newStatus</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">PendingCancel</span>
                <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="n">newStatus</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logEntry</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="n">newStatus</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;cancelOrder: </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">cancelEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">statusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">cancelOrderEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">orderStatusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">newStatus</span> <span class="o">==</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Cancelled</span><span class="p">:</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">cancelledEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;cancelOrder: Unknown orderId </span><span class="si">{</span><span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">trade</span></div>

<div class="viewcode-block" id="IB.reqGlobalCancel"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqGlobalCancel">[docs]</a>    <span class="k">def</span> <span class="nf">reqGlobalCancel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel all active trades including those placed by other</span>
<span class="sd">        clients or TWS/IB gateway.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqGlobalCancel</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;reqGlobalCancel&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqCurrentTime"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqCurrentTime">[docs]</a>    <span class="k">def</span> <span class="nf">reqCurrentTime</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request TWS current time.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqCurrentTimeAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqAccountUpdates"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountUpdates">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdates</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This is called at startup - no need to call again.</span>

<span class="sd">        Request account and portfolio values of the account</span>
<span class="sd">        and keep updated. Returns when both account values and portfolio</span>
<span class="sd">        are filled.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqAccountUpdatesAsync</span><span class="p">(</span><span class="n">account</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqAccountUpdatesMulti"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountUpdatesMulti">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdatesMulti</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        It is recommended to use :meth:`.accountValues` instead.</span>

<span class="sd">        Request account values of multiple accounts and keep updated.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: If specified, filter for this account name.</span>
<span class="sd">            modelCode: If specified, filter for this account model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqAccountUpdatesMultiAsync</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqAccountSummary"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountSummary">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountSummary</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        It is recommended to use :meth:`.accountSummary` instead.</span>

<span class="sd">        Request account values for all accounts and keep them updated.</span>
<span class="sd">        Returns when account summary is filled.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqAccountSummaryAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqAutoOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAutoOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqAutoOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">autoBind</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Bind manual TWS orders so that they can be managed from this client.</span>
<span class="sd">        The clientId must be 0 and the TWS API setting &quot;Use negative numbers</span>
<span class="sd">        to bind automatic orders&quot; must be checked.</span>

<span class="sd">        This request is automatically called when clientId=0.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/open_orders.html</span>
<span class="sd">        https://interactivebrokers.github.io/tws-api/modifying_orders.html</span>

<span class="sd">        Args:</span>
<span class="sd">            autoBind: Set binding on or off.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqAutoOpenOrders</span><span class="p">(</span><span class="n">autoBind</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request and return a list of open orders.</span>

<span class="sd">        This method can give stale information where a new open order is not</span>
<span class="sd">        reported or an already filled or cancelled order is reported as open.</span>
<span class="sd">        It is recommended to use the more reliable and much faster</span>
<span class="sd">        :meth:`.openTrades` or :meth:`.openOrders` methods instead.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqOpenOrdersAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqAllOpenOrders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAllOpenOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqAllOpenOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request and return a list of all open orders over all clients.</span>
<span class="sd">        Note that the orders of other clients will not be kept in sync,</span>
<span class="sd">        use the master clientId mechanism instead to see other</span>
<span class="sd">        client&#39;s orders that are kept in sync.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqAllOpenOrdersAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqCompletedOrders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqCompletedOrders">[docs]</a>    <span class="k">def</span> <span class="nf">reqCompletedOrders</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiOnly</span><span class="p">:</span> <span class="nb">bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Trade</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request and return a list of completed trades.</span>

<span class="sd">        Args:</span>
<span class="sd">            apiOnly: Request only API orders (not manually placed TWS orders).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqCompletedOrdersAsync</span><span class="p">(</span><span class="n">apiOnly</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqExecutions"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqExecutions">[docs]</a>    <span class="k">def</span> <span class="nf">reqExecutions</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">execFilter</span><span class="p">:</span> <span class="n">ExecutionFilter</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Fill</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        It is recommended to use :meth:`.fills`  or</span>
<span class="sd">        :meth:`.executions` instead.</span>

<span class="sd">        Request and return a list of fills.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            execFilter: If specified, return executions that match the filter.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqExecutionsAsync</span><span class="p">(</span><span class="n">execFilter</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqPositions"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqPositions">[docs]</a>    <span class="k">def</span> <span class="nf">reqPositions</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">Position</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        It is recommended to use :meth:`.positions` instead.</span>

<span class="sd">        Request and return a list of positions for all accounts.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqPositionsAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqPnL"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqPnL">[docs]</a>    <span class="k">def</span> <span class="nf">reqPnL</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">PnL</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Start a subscription for profit and loss events.</span>

<span class="sd">        Returns a :class:`.PnL` object that is kept live updated.</span>
<span class="sd">        The result can also be queried from :meth:`.pnl`.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/pnl.html</span>

<span class="sd">        Args:</span>
<span class="sd">            account: Subscribe to this account.</span>
<span class="sd">            modelCode: If specified, filter for this account model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlKey2ReqId</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlKey2ReqId</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="n">PnL</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnL</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">pnl</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqPnL</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pnl</span></div>

<div class="viewcode-block" id="IB.cancelPnL"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelPnL">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPnL</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel PnL subscription.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: Cancel for this account.</span>
<span class="sd">            modelCode: If specified, cancel for this account model.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlKey2ReqId</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reqId</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelPnL</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnL</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s1">&#39;cancelPnL: No subscription for &#39;</span>
                <span class="sa">f</span><span class="s1">&#39;account </span><span class="si">{</span><span class="n">account</span><span class="si">}</span><span class="s1">, modelCode </span><span class="si">{</span><span class="n">modelCode</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqPnLSingle"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqPnLSingle">[docs]</a>    <span class="k">def</span> <span class="nf">reqPnLSingle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">conId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">PnLSingle</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Start a subscription for profit and loss events for single positions.</span>

<span class="sd">        Returns a :class:`.PnLSingle` object that is kept live updated.</span>
<span class="sd">        The result can also be queried from :meth:`.pnlSingle`.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/pnl.html</span>

<span class="sd">        Args:</span>
<span class="sd">            account: Subscribe to this account.</span>
<span class="sd">            modelCode: Filter for this account model.</span>
<span class="sd">            conId: Filter for this contract ID.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conId</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlSingleKey2ReqId</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlSingleKey2ReqId</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="n">pnlSingle</span> <span class="o">=</span> <span class="n">PnLSingle</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnlSingle</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">pnlSingle</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqPnLSingle</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conId</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">pnlSingle</span></div>

<div class="viewcode-block" id="IB.cancelPnLSingle"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelPnLSingle">[docs]</a>    <span class="k">def</span> <span class="nf">cancelPnLSingle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">conId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel PnLSingle subscription for the given account, modelCode</span>
<span class="sd">        and conId.</span>

<span class="sd">        Args:</span>
<span class="sd">            account: Cancel for this account name.</span>
<span class="sd">            modelCode: Cancel for this account model.</span>
<span class="sd">            conId: Cancel for this contract ID.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="n">conId</span><span class="p">)</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">pnlSingleKey2ReqId</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reqId</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelPnLSingle</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2PnlSingle</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s1">&#39;cancelPnLSingle: No subscription for &#39;</span>
                <span class="sa">f</span><span class="s1">&#39;account </span><span class="si">{</span><span class="n">account</span><span class="si">}</span><span class="s1">, modelCode </span><span class="si">{</span><span class="n">modelCode</span><span class="si">}</span><span class="s1">, conId </span><span class="si">{</span><span class="n">conId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqContractDetails"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqContractDetails">[docs]</a>    <span class="k">def</span> <span class="nf">reqContractDetails</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">ContractDetails</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a list of contract details that match the given contract.</span>
<span class="sd">        If the returned list is empty then the contract is not known;</span>
<span class="sd">        If the list has multiple values then the contract is ambiguous.</span>

<span class="sd">        The fully qualified contract is available in the the</span>
<span class="sd">        ContractDetails.contract attribute.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/contract_details.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: The contract to get details for.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqContractDetailsAsync</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqMatchingSymbols"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMatchingSymbols">[docs]</a>    <span class="k">def</span> <span class="nf">reqMatchingSymbols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pattern</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">ContractDescription</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request contract descriptions of contracts that match a pattern.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/matching_symbols.html</span>

<span class="sd">        Args:</span>
<span class="sd">            pattern: The first few letters of the ticker symbol, or for</span>
<span class="sd">                longer strings a character sequence matching a word in</span>
<span class="sd">                the security name.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqMatchingSymbolsAsync</span><span class="p">(</span><span class="n">pattern</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqMarketRule"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMarketRule">[docs]</a>    <span class="k">def</span> <span class="nf">reqMarketRule</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketRuleId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">PriceIncrement</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request price increments rule.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/minimum_increment.html</span>

<span class="sd">        Args:</span>
<span class="sd">            marketRuleId: ID of market rule.</span>
<span class="sd">                The market rule IDs for a contract can be obtained</span>
<span class="sd">                via :meth:`.reqContractDetails` from</span>
<span class="sd">                :class:`.ContractDetails`.marketRuleIds,</span>
<span class="sd">                which contains a comma separated string of market rule IDs.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqMarketRuleAsync</span><span class="p">(</span><span class="n">marketRuleId</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqRealTimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqRealTimeBars">[docs]</a>    <span class="k">def</span> <span class="nf">reqRealTimeBars</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">barSize</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
            <span class="n">realTimeBarsOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">RealTimeBarList</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request realtime 5 second bars.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/realtime_bars.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            barSize: Must be 5.</span>
<span class="sd">            whatToShow: Specifies the source for constructing bars.</span>
<span class="sd">                Can be &#39;TRADES&#39;, &#39;MIDPOINT&#39;, &#39;BID&#39; or &#39;ASK&#39;.</span>
<span class="sd">            useRTH: If True then only show data from within Regular</span>
<span class="sd">                Trading Hours, if False then show all data.</span>
<span class="sd">            realTimeBarsOptions: Unknown.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">bars</span> <span class="o">=</span> <span class="n">RealTimeBarList</span><span class="p">()</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">reqId</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">contract</span> <span class="o">=</span> <span class="n">contract</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">barSize</span> <span class="o">=</span> <span class="n">barSize</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">whatToShow</span> <span class="o">=</span> <span class="n">whatToShow</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">useRTH</span> <span class="o">=</span> <span class="n">useRTH</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">realTimeBarsOptions</span> <span class="o">=</span> <span class="n">realTimeBarsOptions</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startSubscription</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">bars</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqRealTimeBars</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">barSize</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">realTimeBarsOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">bars</span></div>

<div class="viewcode-block" id="IB.cancelRealTimeBars"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelRealTimeBars">[docs]</a>    <span class="k">def</span> <span class="nf">cancelRealTimeBars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bars</span><span class="p">:</span> <span class="n">RealTimeBarList</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel the realtime bars subscription.</span>

<span class="sd">        Args:</span>
<span class="sd">            bars: The bar list that was obtained from ``reqRealTimeBars``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelRealTimeBars</span><span class="p">(</span><span class="n">bars</span><span class="o">.</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endSubscription</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqHistoricalData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalData">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">],</span>
            <span class="n">durationStr</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">keepUpToDate</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
            <span class="n">chartOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[],</span>
            <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">60</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">BarDataList</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request historical bar data.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/historical_bars.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            endDateTime: Can be set to &#39;&#39; to indicate the current time,</span>
<span class="sd">                or it can be given as a datetime.date or datetime.datetime,</span>
<span class="sd">                or it can be given as a string in &#39;yyyyMMdd HH:mm:ss&#39; format.</span>
<span class="sd">                If no timezone is given then the TWS login timezone is used.</span>
<span class="sd">            durationStr: Time span of all the bars. Examples:</span>
<span class="sd">                &#39;60 S&#39;, &#39;30 D&#39;, &#39;13 W&#39;, &#39;6 M&#39;, &#39;10 Y&#39;.</span>
<span class="sd">            barSizeSetting: Time period of one bar. Must be one of:</span>
<span class="sd">                &#39;1 secs&#39;, &#39;5 secs&#39;, &#39;10 secs&#39; 15 secs&#39;, &#39;30 secs&#39;,</span>
<span class="sd">                &#39;1 min&#39;, &#39;2 mins&#39;, &#39;3 mins&#39;, &#39;5 mins&#39;, &#39;10 mins&#39;, &#39;15 mins&#39;,</span>
<span class="sd">                &#39;20 mins&#39;, &#39;30 mins&#39;,</span>
<span class="sd">                &#39;1 hour&#39;, &#39;2 hours&#39;, &#39;3 hours&#39;, &#39;4 hours&#39;, &#39;8 hours&#39;,</span>
<span class="sd">                &#39;1 day&#39;, &#39;1 week&#39;, &#39;1 month&#39;.</span>
<span class="sd">            whatToShow: Specifies the source for constructing bars.</span>
<span class="sd">                Must be one of:</span>
<span class="sd">                &#39;TRADES&#39;, &#39;MIDPOINT&#39;, &#39;BID&#39;, &#39;ASK&#39;, &#39;BID_ASK&#39;,</span>
<span class="sd">                &#39;ADJUSTED_LAST&#39;, &#39;HISTORICAL_VOLATILITY&#39;,</span>
<span class="sd">                &#39;OPTION_IMPLIED_VOLATILITY&#39;, &#39;REBATE_RATE&#39;, &#39;FEE_RATE&#39;,</span>
<span class="sd">                &#39;YIELD_BID&#39;, &#39;YIELD_ASK&#39;, &#39;YIELD_BID_ASK&#39;, &#39;YIELD_LAST&#39;.</span>
<span class="sd">            useRTH: If True then only show data from within Regular</span>
<span class="sd">                Trading Hours, if False then show all data.</span>
<span class="sd">            formatDate: For an intraday request setting to 2 will cause</span>
<span class="sd">                the returned date fields to be timezone-aware</span>
<span class="sd">                datetime.datetime with UTC timezone, instead of local timezone</span>
<span class="sd">                as used by TWS.</span>
<span class="sd">            keepUpToDate: If True then a realtime subscription is started</span>
<span class="sd">                to keep the bars updated; ``endDateTime`` must be set</span>
<span class="sd">                empty (&#39;&#39;) then.</span>
<span class="sd">            chartOptions: Unknown.</span>
<span class="sd">            timeout: Timeout in seconds after which to cancel the request</span>
<span class="sd">                and return an empty bar series. Set to ``0`` to wait</span>
<span class="sd">                indefinitely.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqHistoricalDataAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span> <span class="n">durationStr</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span>
                <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">,</span> <span class="n">keepUpToDate</span><span class="p">,</span> <span class="n">chartOptions</span><span class="p">,</span> <span class="n">timeout</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.cancelHistoricalData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelHistoricalData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelHistoricalData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bars</span><span class="p">:</span> <span class="n">BarDataList</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel the update subscription for the historical bars.</span>

<span class="sd">        Args:</span>
<span class="sd">            bars: The bar list that was obtained from ``reqHistoricalData``</span>
<span class="sd">                with a keepUpToDate subscription.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelHistoricalData</span><span class="p">(</span><span class="n">bars</span><span class="o">.</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endSubscription</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqHistoricalTicks"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalTicks">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalTicks</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">startDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">numberOfTicks</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">useRth</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
            <span class="n">ignoreSize</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
            <span class="n">miscOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request historical ticks. The time resolution of the ticks</span>
<span class="sd">        is one second.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/historical_time_and_sales.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to query.</span>
<span class="sd">            startDateTime: Can be given as a datetime.date or</span>
<span class="sd">                datetime.datetime, or it can be given as a string in</span>
<span class="sd">                &#39;yyyyMMdd HH:mm:ss&#39; format.</span>
<span class="sd">                If no timezone is given then the TWS login timezone is used.</span>
<span class="sd">            endDateTime: One of ``startDateTime`` or ``endDateTime`` can</span>
<span class="sd">                be given, the other must be blank.</span>
<span class="sd">            numberOfTicks: Number of ticks to request (1000 max). The actual</span>
<span class="sd">                result can contain a bit more to accommodate all ticks in</span>
<span class="sd">                the latest second.</span>
<span class="sd">            whatToShow: One of &#39;Bid_Ask&#39;, &#39;Midpoint&#39; or &#39;Trades&#39;.</span>
<span class="sd">            useRTH: If True then only show data from within Regular</span>
<span class="sd">                Trading Hours, if False then show all data.</span>
<span class="sd">            ignoreSize: Ignore bid/ask ticks that only update the size.</span>
<span class="sd">            miscOptions: Unknown.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqHistoricalTicksAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span>
                <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRth</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">,</span> <span class="n">miscOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqMarketDataType"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMarketDataType">[docs]</a>    <span class="k">def</span> <span class="nf">reqMarketDataType</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketDataType</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set the market data type used for :meth:`.reqMktData`.</span>

<span class="sd">        Args:</span>
<span class="sd">            marketDataType: One of:</span>

<span class="sd">                * 1 = Live</span>
<span class="sd">                * 2 = Frozen</span>
<span class="sd">                * 3 = Delayed</span>
<span class="sd">                * 4 = Delayed frozen</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/market_data_type.html</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMarketDataType</span><span class="p">(</span><span class="n">marketDataType</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqHeadTimeStamp"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHeadTimeStamp">[docs]</a>    <span class="k">def</span> <span class="nf">reqHeadTimeStamp</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the datetime of earliest available historical data</span>
<span class="sd">        for the contract.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            useRTH: If True then only show data from within Regular</span>
<span class="sd">                Trading Hours, if False then show all data.</span>
<span class="sd">            formatDate: If set to 2 then the result is returned as a</span>
<span class="sd">                timezone-aware datetime.datetime with UTC timezone.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqHeadTimeStampAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqMktData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMktData">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">genericTickList</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
            <span class="n">snapshot</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">regulatorySnapshot</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
            <span class="n">mktDataOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Ticker</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Subscribe to tick data or request a snapshot.</span>
<span class="sd">        Returns the Ticker that holds the market data. The ticker will</span>
<span class="sd">        initially be empty and gradually (after a couple of seconds)</span>
<span class="sd">        be filled.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/md_request.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            genericTickList: Comma separated IDs of desired</span>
<span class="sd">                generic ticks that will cause corresponding Ticker fields</span>
<span class="sd">                to be filled:</span>

<span class="sd">                =====  ================================================</span>
<span class="sd">                ID     Ticker fields</span>
<span class="sd">                =====  ================================================</span>
<span class="sd">                100    ``putVolume``, ``callVolume`` (for options)</span>
<span class="sd">                101    ``putOpenInterest``, ``callOpenInterest`` (for options)</span>
<span class="sd">                104    ``histVolatility`` (for options)</span>
<span class="sd">                105    ``avOptionVolume`` (for options)</span>
<span class="sd">                106    ``impliedVolatility`` (for options)</span>
<span class="sd">                162    ``indexFuturePremium``</span>
<span class="sd">                165    ``low13week``, ``high13week``, ``low26week``,</span>
<span class="sd">                       ``high26week``, ``low52week``, ``high52week``,</span>
<span class="sd">                       ``avVolume``</span>
<span class="sd">                221    ``markPrice``</span>
<span class="sd">                225    ``auctionVolume``, ``auctionPrice``,</span>
<span class="sd">                       ``auctionImbalance``</span>
<span class="sd">                233    ``last``, ``lastSize``, ``rtVolume``, ``rtTime``,</span>
<span class="sd">                       ``vwap`` (Time &amp; Sales)</span>
<span class="sd">                236    ``shortableShares``</span>
<span class="sd">                258    ``fundamentalRatios`` (of type</span>
<span class="sd">                       :class:`ib_insync.objects.FundamentalRatios`)</span>
<span class="sd">                293    ``tradeCount``</span>
<span class="sd">                294    ``tradeRate``</span>
<span class="sd">                295    ``volumeRate``</span>
<span class="sd">                375    ``rtTradeVolume``</span>
<span class="sd">                411    ``rtHistVolatility``</span>
<span class="sd">                456    ``dividends`` (of type</span>
<span class="sd">                       :class:`ib_insync.objects.Dividends`)</span>
<span class="sd">                588    ``futuresOpenInterest``</span>
<span class="sd">                =====  ================================================</span>

<span class="sd">            snapshot: If True then request a one-time snapshot, otherwise</span>
<span class="sd">                subscribe to a stream of realtime tick data.</span>
<span class="sd">            regulatorySnapshot: Request NBBO snapshot (may incur a fee).</span>
<span class="sd">            mktDataOptions: Unknown</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startTicker</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;mktData&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">genericTickList</span><span class="p">,</span> <span class="n">snapshot</span><span class="p">,</span>
            <span class="n">regulatorySnapshot</span><span class="p">,</span> <span class="n">mktDataOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ticker</span></div>

<div class="viewcode-block" id="IB.cancelMktData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelMktData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelMktData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Unsubscribe from realtime streaming tick data.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: The exact contract object that was used to</span>
<span class="sd">                subscribe with.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ticker</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endTicker</span><span class="p">(</span><span class="n">ticker</span><span class="p">,</span> <span class="s1">&#39;mktData&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reqId</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelMktData</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s1">&#39;cancelMktData: &#39;</span> <span class="sa">f</span><span class="s1">&#39;No reqId found for contract </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqTickByTickData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqTickByTickData">[docs]</a>    <span class="k">def</span> <span class="nf">reqTickByTickData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">numberOfTicks</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Ticker</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Subscribe to tick-by-tick data and return the Ticker that</span>
<span class="sd">        holds the ticks in ticker.tickByTicks.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/tick_data.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            tickType: One of  &#39;Last&#39;, &#39;AllLast&#39;, &#39;BidAsk&#39; or &#39;MidPoint&#39;.</span>
<span class="sd">            numberOfTicks: Number of ticks or 0 for unlimited.</span>
<span class="sd">            ignoreSize: Ignore bid/ask ticks that only update the size.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startTicker</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqTickByTickData</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">ignoreSize</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ticker</span></div>

<div class="viewcode-block" id="IB.cancelTickByTickData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelTickByTickData">[docs]</a>    <span class="k">def</span> <span class="nf">cancelTickByTickData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Unsubscribe from tick-by-tick data</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: The exact contract object that was used to</span>
<span class="sd">                subscribe with.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ticker</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endTicker</span><span class="p">(</span><span class="n">ticker</span><span class="p">,</span> <span class="n">tickType</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reqId</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelTickByTickData</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="sa">f</span><span class="s1">&#39;cancelMktData: No reqId found for contract </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqMktDepthExchanges"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMktDepthExchanges">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktDepthExchanges</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">DepthMktDataDescription</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get those exchanges that have have multiple market makers</span>
<span class="sd">        (and have ticks returned with marketMaker info).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqMktDepthExchangesAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqMktDepth"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMktDepth">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktDepth</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">numRows</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">5</span><span class="p">,</span>
            <span class="n">isSmartDepth</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">mktDepthOptions</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Ticker</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Subscribe to market depth data (a.k.a. DOM, L2 or order book).</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/market_depth.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract of interest.</span>
<span class="sd">            numRows: Number of depth level on each side of the order book</span>
<span class="sd">                (5 max).</span>
<span class="sd">            isSmartDepth: Consolidate the order book across exchanges.</span>
<span class="sd">            mktDepthOptions: Unknown.</span>

<span class="sd">        Returns:</span>
<span class="sd">            The Ticker that holds the market depth in ``ticker.domBids``</span>
<span class="sd">            and ``ticker.domAsks`` and the list of MktDepthData in</span>
<span class="sd">            ``ticker.domTicks``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startTicker</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;mktDepth&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMktDepth</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">numRows</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">,</span> <span class="n">mktDepthOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">ticker</span></div>

<div class="viewcode-block" id="IB.cancelMktDepth"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelMktDepth">[docs]</a>    <span class="k">def</span> <span class="nf">cancelMktDepth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Unsubscribe from market depth data.</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: The exact contract object that was used to</span>
<span class="sd">                subscribe with.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ticker</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endTicker</span><span class="p">(</span><span class="n">ticker</span><span class="p">,</span> <span class="s1">&#39;mktDepth&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">reqId</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelMktDepth</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="sa">f</span><span class="s1">&#39;cancelMktDepth: No reqId found for contract </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqHistogramData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistogramData">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistogramData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">period</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">HistogramData</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Request histogram data.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/histograms.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to query.</span>
<span class="sd">            useRTH: If True then only show data from within Regular</span>
<span class="sd">                Trading Hours, if False then show all data.</span>
<span class="sd">            period: Period of which data is being requested, for example</span>
<span class="sd">                &#39;3 days&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqHistogramDataAsync</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">period</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqFundamentalData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqFundamentalData">[docs]</a>    <span class="k">def</span> <span class="nf">reqFundamentalData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">reportType</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">fundamentalDataOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get fundamental data of a contract in XML format.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/fundamentals.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Contract to query.</span>
<span class="sd">            reportType:</span>

<span class="sd">                * &#39;ReportsFinSummary&#39;: Financial summary</span>
<span class="sd">                * &#39;ReportsOwnership&#39;: Company&#39;s ownership</span>
<span class="sd">                * &#39;ReportSnapshot&#39;: Company&#39;s financial overview</span>
<span class="sd">                * &#39;ReportsFinStatements&#39;: Financial Statements</span>
<span class="sd">                * &#39;RESC&#39;: Analyst Estimates</span>
<span class="sd">                * &#39;CalendarReport&#39;: Company&#39;s calendar</span>
<span class="sd">            fundamentalDataOptions: Unknown</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqFundamentalDataAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">reportType</span><span class="p">,</span> <span class="n">fundamentalDataOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqScannerData"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqScannerData">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">subscription</span><span class="p">:</span> <span class="n">ScannerSubscription</span><span class="p">,</span>
            <span class="n">scannerSubscriptionOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[],</span>
            <span class="n">scannerSubscriptionFilterOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> \
            <span class="n">ScanDataList</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Do a blocking market scan by starting a subscription and canceling it</span>
<span class="sd">        after the initial list of results are in.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/market_scanners.html</span>

<span class="sd">        Args:</span>
<span class="sd">            subscription: Basic filters.</span>
<span class="sd">            scannerSubscriptionOptions: Unknown.</span>
<span class="sd">            scannerSubscriptionFilterOptions: Advanced generic filters.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqScannerDataAsync</span><span class="p">(</span>
                <span class="n">subscription</span><span class="p">,</span> <span class="n">scannerSubscriptionOptions</span><span class="p">,</span>
                <span class="n">scannerSubscriptionFilterOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqScannerSubscription"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqScannerSubscription">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerSubscription</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">subscription</span><span class="p">:</span> <span class="n">ScannerSubscription</span><span class="p">,</span>
            <span class="n">scannerSubscriptionOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[],</span>
            <span class="n">scannerSubscriptionFilterOptions</span><span class="p">:</span>
            <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">ScanDataList</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Subscribe to market scan data.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/market_scanners.html</span>

<span class="sd">        Args:</span>
<span class="sd">            subscription: What to scan for.</span>
<span class="sd">            scannerSubscriptionOptions: Unknown.</span>
<span class="sd">            scannerSubscriptionFilterOptions: Unknown.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">dataList</span> <span class="o">=</span> <span class="n">ScanDataList</span><span class="p">()</span>
        <span class="n">dataList</span><span class="o">.</span><span class="n">reqId</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="n">dataList</span><span class="o">.</span><span class="n">subscription</span> <span class="o">=</span> <span class="n">subscription</span>
        <span class="n">dataList</span><span class="o">.</span><span class="n">scannerSubscriptionOptions</span> <span class="o">=</span> <span class="n">scannerSubscriptionOptions</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="n">dataList</span><span class="o">.</span><span class="n">scannerSubscriptionFilterOptions</span> <span class="o">=</span> \
            <span class="n">scannerSubscriptionFilterOptions</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startSubscription</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">dataList</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqScannerSubscription</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">subscription</span><span class="p">,</span> <span class="n">scannerSubscriptionOptions</span><span class="p">,</span>
            <span class="n">scannerSubscriptionFilterOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">dataList</span></div>

<div class="viewcode-block" id="IB.cancelScannerSubscription"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelScannerSubscription">[docs]</a>    <span class="k">def</span> <span class="nf">cancelScannerSubscription</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dataList</span><span class="p">:</span> <span class="n">ScanDataList</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Cancel market data subscription.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/market_scanners.html</span>

<span class="sd">        Args:</span>
<span class="sd">            dataList: The scan data list that was obtained from</span>
<span class="sd">                :meth:`.reqScannerSubscription`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelScannerSubscription</span><span class="p">(</span><span class="n">dataList</span><span class="o">.</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endSubscription</span><span class="p">(</span><span class="n">dataList</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqScannerParameters"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqScannerParameters">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerParameters</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Requests an XML list of scanner parameters.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqScannerParametersAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.calculateImpliedVolatility"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.calculateImpliedVolatility">[docs]</a>    <span class="k">def</span> <span class="nf">calculateImpliedVolatility</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">optionPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">implVolOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">OptionComputation</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate the volatility given the option price.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/option_computations.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Option contract.</span>
<span class="sd">            optionPrice: Option price to use in calculation.</span>
<span class="sd">            underPrice: Price of the underlier to use in calculation</span>
<span class="sd">            implVolOptions: Unknown</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calculateImpliedVolatilityAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">optionPrice</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">implVolOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.calculateOptionPrice"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.calculateOptionPrice">[docs]</a>    <span class="k">def</span> <span class="nf">calculateOptionPrice</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">volatility</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">optPrcOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">OptionComputation</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate the option price given the volatility.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/option_computations.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: Option contract.</span>
<span class="sd">            volatility: Option volatility to use in calculation.</span>
<span class="sd">            underPrice: Price of the underlier to use in calculation</span>
<span class="sd">            implVolOptions: Unknown</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">calculateOptionPriceAsync</span><span class="p">(</span>
                <span class="n">contract</span><span class="p">,</span> <span class="n">volatility</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">optPrcOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqSecDefOptParams"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqSecDefOptParams">[docs]</a>    <span class="k">def</span> <span class="nf">reqSecDefOptParams</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">underlyingSymbol</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">futFopExchange</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">underlyingSecType</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">underlyingConId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">OptionChain</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the option chain.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/options.html</span>

<span class="sd">        Args:</span>
<span class="sd">            underlyingSymbol: Symbol of underlier contract.</span>
<span class="sd">            futFopExchange: Exchange (only for ``FuturesOption``, otherwise</span>
<span class="sd">                leave blank).</span>
<span class="sd">            underlyingSecType: The type of the underlying security, like</span>
<span class="sd">                &#39;STK&#39; or &#39;FUT&#39;.</span>
<span class="sd">            underlyingConId: conId of the underlying contract.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqSecDefOptParamsAsync</span><span class="p">(</span>
                <span class="n">underlyingSymbol</span><span class="p">,</span> <span class="n">futFopExchange</span><span class="p">,</span>
                <span class="n">underlyingSecType</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.exerciseOptions"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.exerciseOptions">[docs]</a>    <span class="k">def</span> <span class="nf">exerciseOptions</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">exerciseAction</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">exerciseQuantity</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">override</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Exercise an options contract.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/options.html</span>

<span class="sd">        Args:</span>
<span class="sd">            contract: The option contract to be exercised.</span>
<span class="sd">            exerciseAction:</span>
<span class="sd">                * 1 = exercise the option</span>
<span class="sd">                * 2 = let the option lapse</span>
<span class="sd">            exerciseQuantity: Number of contracts to be exercised.</span>
<span class="sd">            account: Destination account.</span>
<span class="sd">            override:</span>
<span class="sd">                * 0 = no override</span>
<span class="sd">                * 1 = override the system&#39;s natural action</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">exerciseOptions</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">exerciseAction</span><span class="p">,</span> <span class="n">exerciseQuantity</span><span class="p">,</span>
            <span class="n">account</span><span class="p">,</span> <span class="n">override</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqNewsProviders"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqNewsProviders">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsProviders</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">List</span><span class="p">[</span><span class="n">NewsProvider</span><span class="p">]:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a list of news providers.</span>

<span class="sd">        This method is blocking.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqNewsProvidersAsync</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqNewsArticle"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqNewsArticle">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsArticle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">articleId</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">newsArticleOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">NewsArticle</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the body of a news article.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/news.html</span>

<span class="sd">        Args:</span>
<span class="sd">            providerCode: Code indicating news provider, like &#39;BZ&#39; or &#39;FLY&#39;.</span>
<span class="sd">            articleId: ID of the specific article.</span>
<span class="sd">            newsArticleOptions: Unknown.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqNewsArticleAsync</span><span class="p">(</span>
                <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">newsArticleOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqHistoricalNews"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalNews">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalNews</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">conId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">startDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">totalResults</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">historicalNewsOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">HistoricalNews</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get historical news headline.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/news.html</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            conId: Search news articles for contract with this conId.</span>
<span class="sd">            providerCodes: A &#39;+&#39;-separated list of provider codes, like</span>
<span class="sd">                &#39;BZ+FLY&#39;.</span>
<span class="sd">            startDateTime: The (exclusive) start of the date range.</span>
<span class="sd">                Can be given as a datetime.date or datetime.datetime,</span>
<span class="sd">                or it can be given as a string in &#39;yyyyMMdd HH:mm:ss&#39; format.</span>
<span class="sd">                If no timezone is given then the TWS login timezone is used.</span>
<span class="sd">            endDateTime: The (inclusive) end of the date range.</span>
<span class="sd">                Can be given as a datetime.date or datetime.datetime,</span>
<span class="sd">                or it can be given as a string in &#39;yyyyMMdd HH:mm:ss&#39; format.</span>
<span class="sd">                If no timezone is given then the TWS login timezone is used.</span>
<span class="sd">            totalResults: Maximum number of headlines to fetch (300 max).</span>
<span class="sd">            historicalNewsOptions: Unknown.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reqHistoricalNewsAsync</span><span class="p">(</span>
                <span class="n">conId</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">,</span> <span class="n">startDateTime</span><span class="p">,</span> <span class="n">endDateTime</span><span class="p">,</span>
                <span class="n">totalResults</span><span class="p">,</span> <span class="n">historicalNewsOptions</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.reqNewsBulletins"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqNewsBulletins">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsBulletins</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">allMessages</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Subscribe to IB news bulletins.</span>

<span class="sd">        https://interactivebrokers.github.io/tws-api/news.html</span>

<span class="sd">        Args:</span>
<span class="sd">            allMessages: If True then fetch all messages for the day.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqNewsBulletins</span><span class="p">(</span><span class="n">allMessages</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.cancelNewsBulletins"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.cancelNewsBulletins">[docs]</a>    <span class="k">def</span> <span class="nf">cancelNewsBulletins</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Cancel subscription to IB news bulletins.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelNewsBulletins</span><span class="p">()</span></div>

<div class="viewcode-block" id="IB.requestFA"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.requestFA">[docs]</a>    <span class="k">def</span> <span class="nf">requestFA</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">faDataType</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Requests to change the FA configuration.</span>

<span class="sd">        This method is blocking.</span>

<span class="sd">        Args:</span>
<span class="sd">            faDataType:</span>

<span class="sd">                * 1 = Groups: Offer traders a way to create a group of</span>
<span class="sd">                  accounts and apply a single allocation method to all</span>
<span class="sd">                  accounts in the group.</span>
<span class="sd">                * 2 = Profiles: Let you allocate shares on an</span>
<span class="sd">                  account-by-account basis using a predefined calculation</span>
<span class="sd">                  value.</span>
<span class="sd">                * 3 = Account Aliases: Let you easily identify the accounts</span>
<span class="sd">                  by meaningful names rather than account numbers.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_run</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">requestFAAsync</span><span class="p">(</span><span class="n">faDataType</span><span class="p">))</span></div>

<div class="viewcode-block" id="IB.replaceFA"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.replaceFA">[docs]</a>    <span class="k">def</span> <span class="nf">replaceFA</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">faDataType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">xml</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Replaces Financial Advisor&#39;s settings.</span>

<span class="sd">        Args:</span>
<span class="sd">            faDataType: See :meth:`.requestFA`.</span>
<span class="sd">            xml: The XML-formatted configuration string.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">replaceFA</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">faDataType</span><span class="p">,</span> <span class="n">xml</span><span class="p">)</span></div>

    <span class="c1"># now entering the parallel async universe</span>

<div class="viewcode-block" id="IB.connectAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.connectAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">connectAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="n">port</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">7497</span><span class="p">,</span>
            <span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span>
            <span class="n">readonly</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">):</span>
        <span class="n">clientId</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">clientId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">clientId</span> <span class="o">=</span> <span class="n">clientId</span>
        <span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span> <span class="ow">or</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c1"># establish API connection</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">connectAsync</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">clientId</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>

            <span class="c1"># autobind manual orders</span>
            <span class="k">if</span> <span class="n">clientId</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">reqAutoOpenOrders</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

            <span class="n">accounts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getAccounts</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">account</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">accounts</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">account</span> <span class="o">=</span> <span class="n">accounts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

            <span class="c1"># prepare initializing  requests</span>
            <span class="n">reqs</span><span class="p">:</span> <span class="n">Dict</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># name -&gt; request</span>
            <span class="n">reqs</span><span class="p">[</span><span class="s1">&#39;positions&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqPositionsAsync</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">readonly</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">serverVersion</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="mi">150</span><span class="p">:</span>
                <span class="n">reqs</span><span class="p">[</span><span class="s1">&#39;completed orders&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqCompletedOrdersAsync</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">account</span><span class="p">:</span>
                <span class="n">reqs</span><span class="p">[</span><span class="s1">&#39;account updates&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqAccountUpdatesAsync</span><span class="p">(</span><span class="n">account</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">accounts</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">MaxSyncedSubAccounts</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">acc</span> <span class="ow">in</span> <span class="n">accounts</span><span class="p">:</span>
                    <span class="n">reqs</span><span class="p">[</span><span class="sa">f</span><span class="s1">&#39;account updates for </span><span class="si">{</span><span class="n">acc</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">=</span> \
                        <span class="bp">self</span><span class="o">.</span><span class="n">reqAccountUpdatesMultiAsync</span><span class="p">(</span><span class="n">acc</span><span class="p">)</span>

            <span class="c1"># run initializing requests concurrently and log if any times out</span>
            <span class="n">tasks</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">req</span> <span class="ow">in</span> <span class="n">reqs</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span>
            <span class="n">resps</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="o">*</span><span class="n">tasks</span><span class="p">,</span> <span class="n">return_exceptions</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">resp</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">reqs</span><span class="p">,</span> <span class="n">resps</span><span class="p">):</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">resp</span><span class="p">,</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">name</span><span class="si">}</span><span class="s1"> request timed out&#39;</span><span class="p">)</span>

            <span class="c1"># the request for executions must come after all orders are in</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqExecutionsAsync</span><span class="p">(),</span> <span class="n">timeout</span><span class="p">)</span>

            <span class="c1"># final check if socket is still ready</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">isReady</span><span class="p">():</span>
                <span class="k">raise</span> <span class="ne">ConnectionError</span><span class="p">(</span>
                    <span class="s1">&#39;Socket connection broken while connecting&#39;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Synchronization complete&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">connectedEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">BaseException</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
            <span class="k">raise</span>
        <span class="k">return</span> <span class="bp">self</span></div>

<div class="viewcode-block" id="IB.qualifyContractsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.qualifyContractsAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">qualifyContractsAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">contracts</span><span class="p">:</span> <span class="n">Contract</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">List</span><span class="p">[</span><span class="n">Contract</span><span class="p">]:</span>
        <span class="n">detailsLists</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span>
            <span class="o">*</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reqContractDetailsAsync</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">contracts</span><span class="p">))</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">contract</span><span class="p">,</span> <span class="n">detailsList</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">contracts</span><span class="p">,</span> <span class="n">detailsLists</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">detailsList</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Unknown contract: </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">detailsList</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">possibles</span> <span class="o">=</span> <span class="p">[</span><span class="n">details</span><span class="o">.</span><span class="n">contract</span> <span class="k">for</span> <span class="n">details</span> <span class="ow">in</span> <span class="n">detailsList</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s1">&#39;Ambiguous contract: </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">, &#39;</span>
                    <span class="sa">f</span><span class="s1">&#39;possibles are </span><span class="si">{</span><span class="n">possibles</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">c</span> <span class="o">=</span> <span class="n">detailsList</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">contract</span>
                <span class="n">expiry</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">lastTradeDateOrContractMonth</span>
                <span class="k">if</span> <span class="n">expiry</span><span class="p">:</span>
                    <span class="c1"># remove time and timezone part as it will cause problems</span>
                    <span class="n">expiry</span> <span class="o">=</span> <span class="n">expiry</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">lastTradeDateOrContractMonth</span> <span class="o">=</span> <span class="n">expiry</span>
                <span class="k">if</span> <span class="n">contract</span><span class="o">.</span><span class="n">exchange</span> <span class="o">==</span> <span class="s1">&#39;SMART&#39;</span><span class="p">:</span>
                    <span class="c1"># overwriting &#39;SMART&#39; exchange can create invalid contract</span>
                    <span class="n">c</span><span class="o">.</span><span class="n">exchange</span> <span class="o">=</span> <span class="n">contract</span><span class="o">.</span><span class="n">exchange</span>
                <span class="n">util</span><span class="o">.</span><span class="n">dataclassUpdate</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
                <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">contract</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">result</span></div>

<div class="viewcode-block" id="IB.reqTickersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqTickersAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqTickersAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">contracts</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">regulatorySnapshot</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">List</span><span class="p">[</span><span class="n">Ticker</span><span class="p">]:</span>
        <span class="n">futures</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">tickers</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">reqIds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">contract</span> <span class="ow">in</span> <span class="n">contracts</span><span class="p">:</span>
            <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
            <span class="n">reqIds</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
            <span class="n">futures</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">future</span><span class="p">)</span>
            <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startTicker</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;snapshot&#39;</span><span class="p">)</span>
            <span class="n">tickers</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMktData</span><span class="p">(</span>
                <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="n">regulatorySnapshot</span><span class="p">,</span> <span class="p">[])</span>
        <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="o">*</span><span class="n">futures</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">ticker</span> <span class="ow">in</span> <span class="n">tickers</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">endTicker</span><span class="p">(</span><span class="n">ticker</span><span class="p">,</span> <span class="s1">&#39;snapshot&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">reqId</span> <span class="ow">in</span> <span class="n">reqIds</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">tickers</span></div>

<div class="viewcode-block" id="IB.whatIfOrderAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.whatIfOrderAsync">[docs]</a>    <span class="k">def</span> <span class="nf">whatIfOrderAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="n">OrderState</span><span class="p">]:</span>
        <span class="n">whatIfOrder</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>
        <span class="n">whatIfOrder</span><span class="o">.</span><span class="n">whatIf</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">placeOrder</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">whatIfOrder</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqCurrentTimeAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqCurrentTimeAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqCurrentTimeAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;currentTime&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqCurrentTime</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqAccountUpdatesAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountUpdatesAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdatesAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="kc">None</span><span class="p">]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;accountValues&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqAccountUpdates</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">account</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqAccountUpdatesMultiAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountUpdatesMultiAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountUpdatesMultiAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="kc">None</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqAccountUpdatesMulti</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">account</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.accountSummaryAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.accountSummaryAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">accountSummaryAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">List</span><span class="p">[</span><span class="n">AccountValue</span><span class="p">]:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">acctSummary</span><span class="p">:</span>
            <span class="c1"># loaded on demand since it takes ca. 250 ms</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqAccountSummaryAsync</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">account</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">acctSummary</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">v</span><span class="o">.</span><span class="n">account</span> <span class="o">==</span> <span class="n">account</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">acctSummary</span><span class="o">.</span><span class="n">values</span><span class="p">())</span></div>

<div class="viewcode-block" id="IB.reqAccountSummaryAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAccountSummaryAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqAccountSummaryAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="kc">None</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="n">tags</span> <span class="o">=</span> <span class="p">(</span>
            <span class="s1">&#39;AccountType,NetLiquidation,TotalCashValue,SettledCash,&#39;</span>
            <span class="s1">&#39;AccruedCash,BuyingPower,EquityWithLoanValue,&#39;</span>
            <span class="s1">&#39;PreviousEquityWithLoanValue,GrossPositionValue,ReqTEquity,&#39;</span>
            <span class="s1">&#39;ReqTMargin,SMA,InitMarginReq,MaintMarginReq,AvailableFunds,&#39;</span>
            <span class="s1">&#39;ExcessLiquidity,Cushion,FullInitMarginReq,FullMaintMarginReq,&#39;</span>
            <span class="s1">&#39;FullAvailableFunds,FullExcessLiquidity,LookAheadNextChange,&#39;</span>
            <span class="s1">&#39;LookAheadInitMarginReq,LookAheadMaintMarginReq,&#39;</span>
            <span class="s1">&#39;LookAheadAvailableFunds,LookAheadExcessLiquidity,&#39;</span>
            <span class="s1">&#39;HighestSeverity,DayTradesRemaining,DayTradesRemainingT+1,&#39;</span>
            <span class="s1">&#39;DayTradesRemainingT+2,DayTradesRemainingT+3,&#39;</span>
            <span class="s1">&#39;DayTradesRemainingT+4,Leverage,$LEDGER:ALL&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqAccountSummary</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="s1">&#39;All&#39;</span><span class="p">,</span> <span class="n">tags</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqOpenOrdersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqOpenOrdersAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqOpenOrdersAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;openOrders&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqOpenOrders</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqAllOpenOrdersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqAllOpenOrdersAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqAllOpenOrdersAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Order</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;openOrders&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqAllOpenOrders</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqCompletedOrdersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqCompletedOrdersAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqCompletedOrdersAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">apiOnly</span><span class="p">:</span> <span class="nb">bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Trade</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;completedOrders&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqCompletedOrders</span><span class="p">(</span><span class="n">apiOnly</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqExecutionsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqExecutionsAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqExecutionsAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">execFilter</span><span class="p">:</span> <span class="n">ExecutionFilter</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Fill</span><span class="p">]]:</span>
        <span class="n">execFilter</span> <span class="o">=</span> <span class="n">execFilter</span> <span class="ow">or</span> <span class="n">ExecutionFilter</span><span class="p">()</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqExecutions</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">execFilter</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqPositionsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqPositionsAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqPositionsAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">Position</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;positions&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqPositions</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqContractDetailsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqContractDetailsAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqContractDetailsAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">ContractDetails</span><span class="p">]]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqContractDetails</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqMatchingSymbolsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMatchingSymbolsAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqMatchingSymbolsAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">pattern</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">ContractDescription</span><span class="p">]]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMatchingSymbols</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">pattern</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;reqMatchingSymbolsAsync: Timeout&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="IB.reqMarketRuleAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMarketRuleAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqMarketRuleAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">marketRuleId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">PriceIncrement</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;marketRule-</span><span class="si">{</span><span class="n">marketRuleId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMarketRule</span><span class="p">(</span><span class="n">marketRuleId</span><span class="p">)</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;reqMarketRuleAsync: Timeout&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="IB.reqHistoricalDataAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalDataAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqHistoricalDataAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">,</span> <span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">],</span>
            <span class="n">durationStr</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
            <span class="n">formatDate</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">keepUpToDate</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
            <span class="n">chartOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[],</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">60</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">BarDataList</span><span class="p">:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">bars</span> <span class="o">=</span> <span class="n">BarDataList</span><span class="p">()</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">reqId</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">contract</span> <span class="o">=</span> <span class="n">contract</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">endDateTime</span> <span class="o">=</span> <span class="n">endDateTime</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">durationStr</span> <span class="o">=</span> <span class="n">durationStr</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">barSizeSetting</span> <span class="o">=</span> <span class="n">barSizeSetting</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">whatToShow</span> <span class="o">=</span> <span class="n">whatToShow</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">useRTH</span> <span class="o">=</span> <span class="n">useRTH</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">formatDate</span> <span class="o">=</span> <span class="n">formatDate</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">keepUpToDate</span> <span class="o">=</span> <span class="n">keepUpToDate</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">chartOptions</span> <span class="o">=</span> <span class="n">chartOptions</span> <span class="ow">or</span> <span class="p">[]</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">container</span><span class="o">=</span><span class="n">bars</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">keepUpToDate</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startSubscription</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">bars</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">formatIBDatetime</span><span class="p">(</span><span class="n">endDateTime</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">durationStr</span><span class="p">,</span> <span class="n">barSizeSetting</span><span class="p">,</span>
            <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">,</span> <span class="n">keepUpToDate</span><span class="p">,</span> <span class="n">chartOptions</span><span class="p">)</span>
        <span class="n">task</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span> <span class="k">if</span> <span class="n">timeout</span> <span class="k">else</span> <span class="n">future</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">task</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelHistoricalData</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;reqHistoricalData: Timeout for </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">bars</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">bars</span></div>

<div class="viewcode-block" id="IB.reqHistoricalTicksAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalTicksAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistoricalTicksAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">startDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">numberOfTicks</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">useRth</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span>
            <span class="n">ignoreSize</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
            <span class="n">miscOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">formatIBDatetime</span><span class="p">(</span><span class="n">startDateTime</span><span class="p">)</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">formatIBDatetime</span><span class="p">(</span><span class="n">endDateTime</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHistoricalTicks</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span> <span class="n">numberOfTicks</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRth</span><span class="p">,</span>
            <span class="n">ignoreSize</span><span class="p">,</span> <span class="n">miscOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqHeadTimeStampAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHeadTimeStampAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqHeadTimeStampAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHeadTimeStamp</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">whatToShow</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">formatDate</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqMktDepthExchangesAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqMktDepthExchangesAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqMktDepthExchangesAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">DepthMktDataDescription</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;mktDepthExchanges&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqMktDepthExchanges</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqHistogramDataAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistogramDataAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqHistogramDataAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">:</span> <span class="nb">bool</span><span class="p">,</span> <span class="n">period</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">HistogramData</span><span class="p">]]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHistogramData</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">useRTH</span><span class="p">,</span> <span class="n">period</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqFundamentalDataAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqFundamentalDataAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqFundamentalDataAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">reportType</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">fundamentalDataOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqFundamentalData</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">reportType</span><span class="p">,</span> <span class="n">fundamentalDataOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqScannerDataAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqScannerDataAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqScannerDataAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">subscription</span><span class="p">:</span> <span class="n">ScannerSubscription</span><span class="p">,</span>
            <span class="n">scannerSubscriptionOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[],</span>
            <span class="n">scannerSubscriptionFilterOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> \
            <span class="o">-&gt;</span> <span class="n">ScanDataList</span><span class="p">:</span>
        <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqScannerSubscription</span><span class="p">(</span>
            <span class="n">subscription</span><span class="p">,</span> <span class="n">scannerSubscriptionOptions</span> <span class="ow">or</span> <span class="p">[],</span>
            <span class="n">scannerSubscriptionFilterOptions</span> <span class="ow">or</span> <span class="p">[])</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">dataList</span><span class="o">.</span><span class="n">reqId</span><span class="p">,</span> <span class="n">container</span><span class="o">=</span><span class="n">dataList</span><span class="p">)</span>
        <span class="k">await</span> <span class="n">future</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelScannerSubscription</span><span class="p">(</span><span class="n">dataList</span><span class="o">.</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span></div>

<div class="viewcode-block" id="IB.reqScannerParametersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqScannerParametersAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqScannerParametersAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="nb">str</span><span class="p">]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;scannerParams&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqScannerParameters</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.calculateImpliedVolatilityAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.calculateImpliedVolatilityAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">calculateImpliedVolatilityAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">optionPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">implVolOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> \
            <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">calculateImpliedVolatility</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">optionPrice</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">implVolOptions</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;calculateImpliedVolatilityAsync: Timeout&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelCalculateImpliedVolatility</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.calculateOptionPriceAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.calculateOptionPriceAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">calculateOptionPriceAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span>
            <span class="n">volatility</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">optPrcOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">[])</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="n">OptionComputation</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">calculateOptionPrice</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">volatility</span><span class="p">,</span> <span class="n">underPrice</span><span class="p">,</span> <span class="n">optPrcOptions</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;calculateOptionPriceAsync: Timeout&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelCalculateOptionPrice</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span></div>

<div class="viewcode-block" id="IB.reqSecDefOptParamsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqSecDefOptParamsAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqSecDefOptParamsAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">underlyingSymbol</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">futFopExchange</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">underlyingSecType</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">underlyingConId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">OptionChain</span><span class="p">]]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqSecDefOptParams</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">underlyingSymbol</span><span class="p">,</span> <span class="n">futFopExchange</span><span class="p">,</span>
            <span class="n">underlyingSecType</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqNewsProvidersAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqNewsProvidersAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsProvidersAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Awaitable</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">NewsProvider</span><span class="p">]]:</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;newsProviders&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqNewsProviders</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqNewsArticleAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqNewsArticleAsync">[docs]</a>    <span class="k">def</span> <span class="nf">reqNewsArticleAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">articleId</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">newsArticleOptions</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]])</span> <span class="o">-&gt;</span> \
            <span class="n">Awaitable</span><span class="p">[</span><span class="n">NewsArticle</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqNewsArticle</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">newsArticleOptions</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">future</span></div>

<div class="viewcode-block" id="IB.reqHistoricalNewsAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.reqHistoricalNewsAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">reqHistoricalNewsAsync</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">conId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">startDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">endDateTime</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">],</span>
            <span class="n">totalResults</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">historicalNewsOptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">TagValue</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">Optional</span><span class="p">[</span><span class="n">HistoricalNews</span><span class="p">]:</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">getReqId</span><span class="p">()</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">formatIBDatetime</span><span class="p">(</span><span class="n">startDateTime</span><span class="p">)</span>
        <span class="n">end</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">formatIBDatetime</span><span class="p">(</span><span class="n">endDateTime</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHistoricalNews</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">conId</span><span class="p">,</span> <span class="n">providerCodes</span><span class="p">,</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">,</span>
            <span class="n">totalResults</span><span class="p">,</span> <span class="n">historicalNewsOptions</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;reqHistoricalNewsAsync: Timeout&#39;</span><span class="p">)</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="IB.requestFAAsync"><a class="viewcode-back" href="../../api.html#ib_insync.ib.IB.requestFAAsync">[docs]</a>    <span class="k">async</span> <span class="k">def</span> <span class="nf">requestFAAsync</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">faDataType</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">wrapper</span><span class="o">.</span><span class="n">startReq</span><span class="p">(</span><span class="s1">&#39;requestFA&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">requestFA</span><span class="p">(</span><span class="n">faDataType</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">wait_for</span><span class="p">(</span><span class="n">future</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">future</span><span class="o">.</span><span class="n">result</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">TimeoutError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;requestFAAsync: Timeout&#39;</span><span class="p">)</span></div></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span><span class="o">.</span><span class="n">set_debug</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">util</span><span class="o">.</span><span class="n">logToConsole</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
    <span class="n">ib</span> <span class="o">=</span> <span class="n">IB</span><span class="p">()</span>
    <span class="n">ib</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">,</span> <span class="mi">7497</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">ib</span><span class="o">.</span><span class="n">disconnect</span><span class="p">()</span>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>